import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;
import java.util.stream.Collectors;

/**
 * CompleteTest
 *
 * @author: lunjiawang
 * @date: 2019/1/8
 **/
public class Permutate {

    /**
     * 保存生成的排列组合内容
     **/

    public static void main(String[] args) throws Exception {
        System.out.println("\n" +
                "          _____                    _____                    _____            _____           _______                       _____                    _____                    _____                    _____          \n" +
                "         /\\    \\                  /\\    \\                  /\\    \\          /\\    \\         /::\\    \\                     /\\    \\                  /\\    \\                  /\\    \\                  /\\    \\         \n" +
                "        /::\\____\\                /::\\    \\                /::\\____\\        /::\\____\\       /::::\\    \\                   /::\\    \\                /::\\    \\                /::\\    \\                /::\\    \\        \n" +
                "       /:::/    /               /::::\\    \\              /:::/    /       /:::/    /      /::::::\\    \\                  \\:::\\    \\              /::::\\    \\               \\:::\\    \\              /::::\\    \\       \n" +
                "      /:::/    /               /::::::\\    \\            /:::/    /       /:::/    /      /::::::::\\    \\                  \\:::\\    \\            /::::::\\    \\               \\:::\\    \\            /::::::\\    \\      \n" +
                "     /:::/    /               /:::/\\:::\\    \\          /:::/    /       /:::/    /      /:::/~~\\:::\\    \\                  \\:::\\    \\          /:::/\\:::\\    \\               \\:::\\    \\          /:::/\\:::\\    \\     \n" +
                "    /:::/____/               /:::/__\\:::\\    \\        /:::/    /       /:::/    /      /:::/    \\:::\\    \\                  \\:::\\    \\        /:::/__\\:::\\    \\               \\:::\\    \\        /:::/__\\:::\\    \\    \n" +
                "   /::::\\    \\              /::::\\   \\:::\\    \\      /:::/    /       /:::/    /      /:::/    / \\:::\\    \\                 /::::\\    \\      /::::\\   \\:::\\    \\              /::::\\    \\      /::::\\   \\:::\\    \\   \n" +
                "  /::::::\\    \\   _____    /::::::\\   \\:::\\    \\    /:::/    /       /:::/    /      /:::/____/   \\:::\\____\\               /::::::\\    \\    /::::::\\   \\:::\\    \\    ____    /::::::\\    \\    /::::::\\   \\:::\\    \\  \n" +
                " /:::/\\:::\\    \\ /\\    \\  /:::/\\:::\\   \\:::\\    \\  /:::/    /       /:::/    /      |:::|    |     |:::|    |             /:::/\\:::\\    \\  /:::/\\:::\\   \\:::\\____\\  /\\   \\  /:::/\\:::\\    \\  /:::/\\:::\\   \\:::\\____\\ \n" +
                "/:::/  \\:::\\    /::\\____\\/:::/__\\:::\\   \\:::\\____\\/:::/____/       /:::/____/       |:::|____|     |:::|    |            /:::/  \\:::\\____\\/:::/  \\:::\\   \\:::|    |/::\\   \\/:::/  \\:::\\____\\/:::/  \\:::\\   \\:::|    |\n" +
                "\\::/    \\:::\\  /:::/    /\\:::\\   \\:::\\   \\::/    /\\:::\\    \\       \\:::\\    \\        \\:::\\    \\   /:::/    /            /:::/    \\::/    /\\::/   |::::\\  /:::|____|\\:::\\  /:::/    \\::/    /\\::/    \\:::\\  /:::|____|\n" +
                " \\/____/ \\:::\\/:::/    /  \\:::\\   \\:::\\   \\/____/  \\:::\\    \\       \\:::\\    \\        \\:::\\    \\ /:::/    /            /:::/    / \\/____/  \\/____|:::::\\/:::/    /  \\:::\\/:::/    / \\/____/  \\/_____/\\:::\\/:::/    / \n" +
                "          \\::::::/    /    \\:::\\   \\:::\\    \\       \\:::\\    \\       \\:::\\    \\        \\:::\\    /:::/    /            /:::/    /                 |:::::::::/    /    \\::::::/    /                    \\::::::/    /  \n" +
                "           \\::::/    /      \\:::\\   \\:::\\____\\       \\:::\\    \\       \\:::\\    \\        \\:::\\__/:::/    /            /:::/    /                  |::|\\::::/    /      \\::::/____/                      \\::::/    /   \n" +
                "           /:::/    /        \\:::\\   \\::/    /        \\:::\\    \\       \\:::\\    \\        \\::::::::/    /             \\::/    /                   |::| \\::/____/        \\:::\\    \\                       \\::/____/    \n" +
                "          /:::/    /          \\:::\\   \\/____/          \\:::\\    \\       \\:::\\    \\        \\::::::/    /               \\/____/                    |::|  ~|               \\:::\\    \\                       ~~          \n" +
                "         /:::/    /            \\:::\\    \\               \\:::\\    \\       \\:::\\    \\        \\::::/    /                                           |::|   |                \\:::\\    \\                                  \n" +
                "        /:::/    /              \\:::\\____\\               \\:::\\____\\       \\:::\\____\\        \\::/____/                                            \\::|   |                 \\:::\\____\\                                 \n" +
                "        \\::/    /                \\::/    /                \\::/    /        \\::/    /         ~~                                                   \\:|   |                  \\::/    /                                 \n" +
                "         \\/____/                  \\/____/                  \\/____/          \\/____/                                                                \\|___|                   \\/____/                                  \n" +
                "                                                                                                                                                                                                                     \n");
        String readFileName = "D:\\Users\\lunjiawang\\Desktop\\saleproperty.xlsx";
        String writeFileName = "D:\\Users\\lunjiawang\\Desktop\\saleproperty8.xlsx";
        String writeSheetName = "sheet1";
        int sheetIndex = 2;
        int startRow = 2;
        int endRow = 5;
        int startCol = 1;
        int endCol = 12;
//        String str = Arrays.toString(args);
//        List<String> params = Arrays.asList(str.substring(1, str.length()-1).split(","));
//        String readFileName = params.get(0);
//        String writeFileName = params.get(1);
//        String writeSheetName = params.get(2);
//        int sheetIndex = Integer.parseInt(params.get(3));
//        int startRow = Integer.parseInt(params.get(4));
//        int endRow = Integer.parseInt(params.get(5));
//        int startCol = Integer.parseInt(params.get(6));
//        int endCol = Integer.parseInt(params.get(7));
        List<List<String>> sheet = ExcelBuilder.readSheet(readFileName, sheetIndex, startRow, endRow, startCol, endCol);

        Permutate permutate = new Permutate();
        List<String> permutation = permutate.permutation(sheet, "", new ArrayList<>());
        List<String> retSheet = permutation.stream().distinct().collect(Collectors.toList());

        Map<String, List<String>> map = new HashMap<>();
        ExcelBuilder<List<String>> listExcelBuilder = new ExcelBuilder<>(map, writeFileName);
        map.put(writeSheetName, retSheet);
        listExcelBuilder.write();

    }

    /**
     * 递归的方式计算排列组合
     *
     * @param list   传入list.size()个集合
     * @param preStr 上一步递归中生成的排列组合
     * @return
     */
    private List<String> permutation(List<List<String>> list, String preStr, List<String> permutation) {
        int size = list.size();
        if (1 == size) {
            for (int i = 0; i < list.get(0).size(); i++) {
                permutation.add(preStr + list.get(0).get(i));
            }
        } else {
            List<String> permu = new ArrayList<>(list.get(0));
            List<List<String>> now = new ArrayList<>(list);
            now.remove(0);
            for (String aPermu : permu) {
                permutation(now, preStr + aPermu + ",", permutation);
            }
        }
        return permutation;
    }

}
